import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import componentsRouter from './modules/components'
import chartsRouter from './modules/charts'
import tableRouter from './modules/table'
import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'dashboard', icon: 'dashboard', affix: true }
      }
    ]
  },
  // {
  //   path: '/instructions',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'instructions',
  //       component: () => import('@/views/instructions/instructions'),
  //       name: 'instructions',
  //       meta: { title: '操作手册', icon: 'documentation' }
  //     }
  //   ]
  // },
  /* {
    path: '/nested',
    component: Layout,
    name: 'Nested',
    meta: {
      title: '极点引擎',
      icon: 'nested'
    },
    children: [
      {
        path: 'statistics',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'statistics',
        meta: { title: '数据报表1' },
        children: [
          {
            path: 'statistics-change',
            component: () => import('@/views/nested/statistics/statistics-change'),
            name: 'statistics-change',
            meta: { title: '转化报表1' }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'Menu1-2',
            meta: { title: '用户报表1' },
          },
        ]
      },
    ]
  },*/
  {
    path: '/mine',
    component: Layout,
    meta: {
      title: '个人中心',
      icon: 'user',
      roles: ['super_editor'],
    },
    hidden:true,
    children: [
      {
        path: 'mine',
        component: () => import('@/views/mine/mine'),
        name: 'mine',
        meta: { title: '个人中心', icon: '', noCache: false },
        hidden:true
      },
      {
        path: 'password',
        component: () => import('@/views/mine/password'),
        name: 'password',
        meta: { title: '修改密码', icon: '', noCache: false },
        hidden:true
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // 单页面小程序管理
  /* {
    path: '/miniapp',
    component: Layout,
    redirect: '',
    name: 'Table',
    meta: {
      title: '单页面小程序',
      icon: 'table',
      roles: ['super_editor'],
    },
    children: [
      {
        path: 'miniapp-my/:types1',
        component: () => import('@/views/miniapp/miniapp-my'),
        name: 'miniapp-my',
        meta: { title: '我的小程序',roles: ['super_editor']}
      },
      {
        path: 'miniapp-info/:id(\\d+)',
        component: () => import('@/views/miniapp/miniapp-info'),
        name: 'miniapp-info',
        meta: { title: '小程序详情' },
        hidden: true
      },
      {
        path: 'miniapp-upload/:id(\\d+)',
        component: () => import('@/views/miniapp/miniapp-upload'),
        name: 'miniapp-upload',
        meta: { title: '小程序发布' },
        hidden: true
      },
      {
        path: 'miniapp-apply/:id(\\d+)',
        component: () => import('@/views/miniapp/miniapp-apply'),
        name: 'miniapp-apply',
        meta: { title: '小程序提交审核' },
        hidden: true
      },
      {
        path: 'page-list/:id(\\d+)',
        component: () => import('@/views/miniapp/page-list'),
        name: 'page-list',
        meta: { title: '落地页管理' },
        hidden: true
      },
      {
        path: 'user-list',
        component: () => import('@/views/miniapp/user-list'),
        name: 'user-list',
        meta: { title: '用户管理' }
      },
      {
        path: 'form-list',
        component: () => import('@/views/miniapp/form-list'),
        name: 'form-list',
        meta: { title: '表单管理' }
      },
      {
        path: 'form-edit/:id(\\d+)',
        component: () => import('@/views/miniapp/form-edit'),
        name: 'form-edit',
        meta: { title: '表单编辑', noCache: true },
        hidden: true
      },
      {
        path: 'notice-list',
        component: () => import('@/views/miniapp/notice-list'),
        name: 'notice-list',
        meta: { title: '通告管理' }
      },
      {
        path: 'goods-list',
        component: () => import('@/views/miniapp/goods-list'),
        name: 'goods-list',
        meta: { title: '商品管理' }
      },
      {
        path: 'goods-add',
        component: () => import('@/views/miniapp/goods-add'),
        name: 'goods-add',
        meta: { title: '商品添加', noCache: true },
        hidden: true
      },
      {
        path: 'goods-edit/:id(\\d+)',
        component: () => import('@/views/miniapp/goods-add'),
        name: 'goods-edit',
        meta: { title: '商品编辑', noCache: true },
        hidden: true
      },
      {
        path: 'landing-page/:id(\\d+)',
        component: () => import('@/views/miniapp/landing-page'),
        name: 'landing-page',
        meta: { title: '落地页' },
        hidden: true
      },
      {
        path: 'order-list',
        component: () => import('@/views/miniapp/order-list'),
        name: 'order-list',
        meta: { title: '订单管理', noCache: true },
      },
      {
        path: 'order-info/:id(\\d+)',
        component: () => import('@/views/miniapp/order-info'),
        name: 'order-info',
        meta: { title: '订单详情' },
        hidden: true
      },
      {
        path: 'alichat-list',
        component: () => import('@/views/miniapp/alichat-list'),
        name: 'alichat-list',
        meta: { title: '阿里客服管理', noCache: true },
      },
      {
        path: 'alisms',
        component: () => import('@/views/miniapp/alisms'),
        name: 'alisms',
        meta: { title: '阿里短信管理', noCache: true },
      },
      {
        path: 'alipay-template-message-list',
        component: () => import('@/views/miniapp/alipay-template-message-list'),
        name: 'alipay-template-message-list',
        meta: { title: '支付宝模板消息', noCache: true },
      },
    ]
  },
  //商户管理
  {
    path: '/shop',
    component: Layout,
    redirect: '/shop/complex-table',
    name: 'Table',
    meta: {
      title: '商户管理',
      icon: 'table'
    },
    children: [
      {
        path: 'shop-list',
        component: () => import('@/views/shop/shop-list'),
        name: 'shop-list',
        meta: { title: '商户管理' }
      },
      {
        path: 'shop-add',
        component: () => import('@/views/shop/shop-add'),
        name: 'shop-add',
        meta: { title: '商户添加' }
      },
    ]
  },
  //模板市场
  {
    path: '/market',
    component: Layout,
    redirect: '/market/market-list',
    meta: {
      title: '代理商小程序',
      icon: 'table'
    },
    children: [
      {
        path: 'market-list',
        component: () => import('@/views/market/market-list'),
        name: 'market-list',
        meta: { title: '代理小程序' }
      },
      {
        path: 'shop-add',
        component: () => import('@/views/shop/shop-add'),
        name: 'shop-add',
        meta: { title: '订单管理' }
      },
    ]
  },
  //商城管理
  {
    path: '/mall',
    component: Layout,
    redirect: '',
    name: 'Table',
    meta: {
      title: '商城小程序',
      icon: 'table'
    },
    children: [
      {
        path: '/minapp/miniapp-my/:types2',
        component: () => import('@/views/miniapp/miniapp-my'),
        name: 'miniapp-my',
        meta: { title: '我的小程序' }
      },
      {
        path: 'goods-list',
        component: () => import('@/views/mall/goods-list'),
        name: 'goods-list',
        meta: { title: '商品管理' }
      },
    ]
  },
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/page',
    alwaysShow: true, // will always show the root menu
    name: 'Permission',
    meta: {
      title: 'permission',
      icon: 'lock',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'page',
        component: () => import('@/views/permission/page'),
        name: 'PagePermission',
        meta: {
          title: 'pagePermission',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'directive',
        component: () => import('@/views/permission/directive'),
        name: 'DirectivePermission',
        meta: {
          title: 'directivePermission'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'role',
        component: () => import('@/views/permission/role'),
        name: 'RolePermission',
        meta: {
          title: 'rolePermission',
          roles: ['admin']
        }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true } */
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

// 实例化vue的时候只挂载constantRouter
export default new Router({
  routes: constantRoutes
})

